<?php

namespace eu\freeplace\page\model;

use eu\freeplace\page\model\ValueObject as ValueObject;

class FolderVO extends ValueObject {

    const CLASS_NAME = "FolderVO";
    const DB_TABLE   = "folder";

    private $FolderId   = null;
    private $FolderName = null;
    private $Level      = null;
    private $Parent     = null;
    private $blocked    = null;

    public function __construct($folderID="") {
        global $logger;
        $logger->log("Calling instance of ".self::CLASS_NAME."( ".$folderID." )", "NOTICE");

        try {
            if (strlen($folderID) < 1)
                throw new Exception(self::CLASS_NAME . " - could not fetch Folder ".FolderId."]");

            $this->FolderId = $folderID;
        } catch (Exception $e) {

            $logger->log($e->getMessage(), "NOTICE");
        }
        return null;
    }

    public function __clone() {
        self::fetchFolder($this->FolderId);
    }

    public function __toString() {
        return "{/**TODO: implement me */}";
    }

    protected function createDatabaseTable() {
        $sql =
        "CREATE TABLE IF NOT EXISTS ".$this->getTablePrefix().self::DB_TABLE." (
            FolderId   BIGINT    UNIQUE,
            FolderName CHAR(255) NOT NULL,
            Level      INTEGER   NOT NULL DEFAULT 0,
            Parent     CHAR(255) NOT NULL DEFAULT 'root',
            blocked    BOOLEAN   DEFAULT false,

            PRIMARY KEY( FolderId )
        );";
        return $sql;
    }

    public function getClassAttributes() {
        $folder['FolderId']   = $this->FolderId;
        $folder['FolderName'] = $this->FolderName;
        $folder['Level']      = $this->Level;
        $folder['Parent']     = $this->Parent;
        $folder['blocked']    = $this->blocked;
        return $folder;
    }

    private function fetchFolder($folderID) {
        global $logger;

        $table_name = $this->getTablePrefix().self::DB_TABLE;
        $sql = "SELECT * FROM ".$table_name." WHERE FolderId=".$folderID.";";

        $logger->log(self::CLASS_NAME."().fetchTemplate()", "DEBUG");
        $logger->log("\t".$sql, "DEBUG");

        $pdo = $this->getResult($sql);
        $result = $pdo->fetch();

        if($result != 0) {
            $this->FolderId = $result['FolderId'];
            $this->FolderName     = $result['FolderName'];
            $this->Level     = $result['Level'];
            $this->Parent     = $result['Parent'];
            $this->blocked     = $result['blocked'];
        } else {
            $logger->log("\tCould not fetch a template.", "DEBUG");
        }
    }

    ### [TODO]##################################################################


}//CLASS

?>
